<template>
  <div class="main">
    <div class="content">
      <div class="left">
        <p>同济智信馆智慧能源</p>
        <div id="earth" style="padding-top: 20px"></div>
      </div>
      <div class="middle">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" id="myform">
          <div class="login">
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                type="text"
                auto-complete="off" placeholder="请输入登录名" id="account"/>
            </el-form-item>
          </div>
          <div class="login">
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                auto-complete="off" placeholder="请输入密码" id="password"/>
            </el-form-item>
          </div>
          <el-form-item prop="code" v-if="captchaEnabled">
            <div class="login" style="margin-bottom: 50px;display: flex;justify-content: flex-end;align-items: center">
              <el-input
                v-model="loginForm.code"
                auto-complete="off"
                type="text" placeholder="请输入验证码" id="code"
                @keyup.enter.native="handleLogin"/>
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
        </el-form>

      </div>
      <div class="right">
        <div id="btn" title="Login" @click ="handleLogin">
          <div class="btn0"></div>
          <div class="btn1"></div>
          <div class="btn2"></div>
        </div>
      </div>
    </div>
    <div class="warning" style="display: none;">
      <span>×</span>用户名或密码错误
    </div>
  </div>
</template>

<script>
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from '@/utils/jsencrypt'
import * as echarts from "echarts"
import "echarts-gl"
import "../assets/js/world"

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "请输入您的账号"}
        ],
        password: [
          {required: true, trigger: "blur", message: "请输入您的密码"}
        ],
        code: [{required: true, trigger: "change", message: "请输入验证码"}]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
      wechatLogin: false,
      qrcodeUrl: null,
      qrInvalid: false,
      receiveScan: false,
      token: null,
      timer: null
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  mounted() {
    this.initEcharts();
  },
  methods: {
    initEcharts() {
      let dom = document.getElementById("earth");
      const earth = echarts.init(dom);
      //自制纹理贴图
      const canvas = document.createElement('canvas');
      const mapChart = echarts.init(canvas, null, {width: 4096, height: 2048});
      mapChart.setOption({
        backgroundColor: 'rgba(196,195,195,0.8)',
        series: [
          {
            type: 'map',
            map: 'world',
            // 绘制完整尺寸的 echarts 实例
            top: 0, left: 0,
            right: 0, bottom: 0,
            boundingCoords: [[-180, 90], [180, -90]],
            itemStyle: {
              normal: {
                areaColor: 'rgb(0,47,167,1)',
                borderColor: 'rgb(0,0,0)'
              },
              emphasis: {
                areaColor: 'rgba(0,47,167,1)'
              }
            }
          }
        ]
      });
      earth.setOption({
        globe: {
          baseTexture: mapChart,
          viewControl: {
            rotateSensitivity: 0,
            panSensitivity: 0,
            zoomSensitivity: 0
          }
        },
        series: []
      });
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({path: this.redirect || "/"}).catch(() => {
            });
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  background: url("../assets/images/earth0.jpg") no-repeat center;
  background-size: cover;
}
.main{
  height: 100vh;
  width: 100vw;

}

.content {
  width: 845px;
  height: 310px;
  margin: 0 auto;
  position: relative;
  top: 50%;
  margin-top: -125px;
  background: url("../assets/images/login0.png") no-repeat center;
}

.left, .middle, .right {
  width: 33%;
  float: left;
  color: #002FA7;
  text-align: center;
}

.left {
  margin-left: 1%;
  margin-top: 13px;
}

.left p {
  font-size: 18px;
}

.middle {
  margin-top: 80px;
}

.middle .login {
  width: 280px;
  height: 40px;
  margin-bottom: 30px;
  background: url("../assets/images/login1.png") no-repeat center;
}

.middle .login input {
  height: 38px;
  border: none;
  background: none !important;
  outline: none;
  color: #B0CC35;
  text-align: center;
}

.right {
  margin-top: 100px;
  position: relative;
  box-sizing: border-box;
  padding-top: 95px;
  padding-left: 156px;
}

.btn0 {
  position: absolute;
  width: 78px;
  height: 78px;
  background: url("../assets/images/close0.png") no-repeat;
  background-size: 100%;
  animation: clockwise 3s linear infinite;
}

.btn1 {
  position: absolute;
  width: 52px;
  height: 52px;
  margin: 13px;
  background: url("../assets/images/close1.png") no-repeat;
  background-size: 100%;
}

.btn2 {
  position: absolute;
  width: 30px;
  height: 30px;
  margin: 24px;
  background: url("../assets/images/close2.png") no-repeat;
  background-size: 100%;
  animation: anticlockwise 3s linear infinite;
}

@keyframes clockwise {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(-360deg);
  }
}

@keyframes anticlockwise {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

#earth {
  width: 150px;
  height: 150px;
  margin-left: 60px;
}

.warning {
  width: 595px;
  height: 100px;
  background: url("../assets/images/login2.png") no-repeat center;
  margin: 0 auto;
  position: relative;
  color: red;
  line-height: 100px;
  text-align: center;
  font-weight: bold;
}

.warning span {
  animation: 1s bounceIn;
  font-size: 50px;
  display: block;
  position: absolute;
  left: 8%;
  margin-top: -5px;
}

@keyframes bounceIn {
  from, 20%, 40%, 60%, 80%, to {
    animation-timing-function: cubic-bezier(0.215, 0.610, 0.355, 1.000);
  }

  0% {
    opacity: 0;
    transform: scale3d(.3, .3, .3);
  }

  100% {
    transform: scale3d(1, 1, 1);
  }
}

.bounceIn {
  animation: 500ms bounceIn;
}

#btn {
  width: 78px;
  height: 78px;
  cursor: pointer;
}

.login-code-img {
  height: 38px;
  width: 95px;
  padding-right: 15px;
}
</style>
